module("rope", package.seeall);

function Rope()
    rope = {};
    rope[1] = display.newGroup();
    prevBody = 0;

        function rope:RopeBuild(ropeSize, x, y, n, piece, bone)

            prevBody = piece;
            local ropePart = display.newImage("Assets/Rope.png", piece.x - 3, (piece.y - 23) + n * 32.1 )
            ropePart.name = "rope";
            physics.addBody(ropePart,{bodyType = "dynamic", density=15, friction=1, bounce = .2 });
            physics.addBody(bone, {bodyType = "dynamic", density=2, friction=0.5, bounce=.2});
            prevBody = piece;
            rope[1]:insert(ropePart);

            for i = 1, n do
                if(n == 1) then
                    local pieceJoint = physics.newJoint("pivot", piece, ropePart, piece.x, piece.y + 3);

                elseif(n  > 1) then
                    print(n)
                    local ropeJoint = physics.newJoint("pivot", rope[1][n - 1],rope[1][n], piece.x, ropePart.y);
                end
                if(n == 6) then
                    local boneJoint = physics.newJoint("pivot", bone, rope[1][n], piece.x, ropePart.y);
                end
            end
        end
    return rope;
end